package com.jike.jkgame;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ViewSwitcher;

public class TreasureFragment extends Fragment implements OnClickListener {
	
	private static final int SELECT_TREASURE_REQ = 100;
	
	private Button mSnatchBtn;
	private ViewPager mTreasurePager;
	private LayoutInflater mInflater;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mInflater = LayoutInflater.from(getActivity());
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.treasure_layout, container, false);
		mSnatchBtn = (Button) v.findViewById(R.id.btn_snatch);
		mSnatchBtn.setOnClickListener(this);
		
		mTreasurePager = (ViewPager) v.findViewById(R.id.treasure_pager);
		mTreasurePager.setAdapter(new TreaurePagerAdapter());
		
		v.findViewById(R.id.iv_collect).setOnClickListener(this);
		
		mTreasurePager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int postion) {
				
			}
			
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
				
			}
			
			@Override
			public void onPageScrollStateChanged(int state) {
				if (state == ViewPager.SCROLL_STATE_IDLE) {
					
					mTreasurePager.setCurrentItem(1, false);
				}
				
			}
		});
		
		return v;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_snatch:{
			Intent intent = new Intent(getActivity(), SnatchActivity.class);
			startActivity(intent);
			break;
		}
		case R.id.iv_collect:{
			Intent intent = new Intent(getActivity(), TreasureCollectionActivity.class);
			startActivityForResult(intent, SELECT_TREASURE_REQ);
			break;
		}
		default:
			break;
		}
	}
	
	class ItemsAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return 4;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = convertView;

			if (view == null) {
				view = mInflater.inflate(R.layout.strength_item_layout, parent,
						false);
			}

			return view;
		}

	}

	
	class TreaurePagerAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return 3;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			((ViewPager) container).removeViewAt(position);
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			final GridView gv = new GridView(getActivity());
			gv.setLayoutParams(new ViewSwitcher.LayoutParams(
                    android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                    android.view.ViewGroup.LayoutParams.MATCH_PARENT));
			gv.setNumColumns(2);
			gv.setAdapter(new ItemsAdapter());
			container.addView(gv, position);

			return gv;
		}

	}


	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		if(requestCode == SELECT_TREASURE_REQ) {
			if (resultCode == Activity.RESULT_OK) {
				// TODO 更新选择宝物
			}
		}
	}
	
	

}
